import Vue from 'vue'
import Vuex from 'vuex'

import vuexPersist from "vuex-persist"
Vue.use(Vuex)
const Vuexlocal = new vuexPersist({
    storage: window.localStorage,
    key: '2005B_vuex',
})

export default new Vuex.Store({
    state: {
        name: {},
        token: '',
        trueFalse: false,
        username: '',
        password: '',
        user_id: '',
        cartList: [],
        cartLists: [],
    },
    mutations: {
        // 把波尔值还有用户名传进来
        logintrue(state, objs) {
            console.log(objs.trueFalse);
            state.trueFalse = objs.trueFalse;
            state.username = objs.username;
            state.user_id = objs.user_id;
        },
        visibleClick(state, visible) {
            state.trueFalse = visible;
        },
        cartList(state, list) {
            state.cartList = list
            console.log(state.cartList)
        },
        cartLists(state, list) {
            state.cartLists = list
                // console.log(state.cartList)
        }
    },
    getters: {
        num(state) {
            var num = 0;
            state.cartList.forEach(item => {
                num += item.num
            })
            return num;
        },
        nums(state) {
            var nums = 0;
            state.cartLists.forEach(item => {
                nums += item.num
            })
            return nums
        },
        sum(state) {
            var sum = 0;
            state.cartLists.forEach(item => {
                sum += item.num * item.price
            })
            return sum
        }
    },
    modules: {},
    plugins: [Vuexlocal.plugin],
})